import json
import httpx
from agno.agent import Agent
from agno.models.openai.like import OpenAILike
from os import getenv

# from typing import Iterator
# from agno.utils.pprint import pprint_run_response


def get_top_hackernews_stories(num_stories: int = 10) -> str:
    """
    Use this function to get top stories from Hacker News.

    Args:
        num_stories (int): Number of stories to return. Defaults to 10.

    Returns:
        str: JSON string of top stories.
    """

    # Fetch top story IDs
    response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
    story_ids = response.json()

    # Fetch story details
    stories = []
    for story_id in story_ids[:num_stories]:
        story_response = httpx.get(
            f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"
        )
        story = story_response.json()
        if "text" in story:
            story.pop("text", None)
        stories.append(story)
    return json.dumps(stories)


agent = Agent(
    model=OpenAILike(
        # id="qwen3-235b-a22b",
        id="qwen3-max-preview",       
        # id="deepseek-v3.1",
        # api_key=getenv("ALI_API_KEY"),
        api_key=getenv("BAILIAN_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    ),
    tools=[get_top_hackernews_stories],
    show_tool_calls=True,
    markdown=True,
    monitoring=True,
    debug_mode=True,
)

agent.print_response("总结 Hacker News 上的前五个头条新闻", stream=True)
# agent.print_response("Summarize the top 5 stories on hackernews?", stream=True)
# response_stream: Iterator[RunResponse] = agent.run("讲一个机器人的故事，100字以内", stream=True)
# pprint_run_response(response_stream, markdown=True)
